package wx.controller;

import admin.bean.BootStrapExpressData;
import admin.bean.Express;
import admin.bean.Message;
import admin.service.ExpressService;
import admin.service.impl.ExpressServiceImpl;
import exception.ReCodeException;
import mvc.ResponseBody;
import util.DateFormatUtil;
import util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class ExpressController {

    ExpressService service = new ExpressServiceImpl();

    @ResponseBody("/WxExpress/findByUserPhone.do")
    public String findByUserPhone(HttpServletRequest req, HttpServletResponse resp){
        //从session里获取用户的手机号
        String[] userInfo = (String[]) req.getSession().getAttribute("userInfo");
        String userPhone = userInfo[0];
        List<Express> list = service.findByUserPhone(userPhone);
        List<BootStrapExpressData> list1 = new ArrayList<>();
        for(Express e:list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"取件码已清空":e.getCode();
            BootStrapExpressData e2 = new BootStrapExpressData(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());

            list1.add(e2);

        }
        Message msg = new Message();
        if (list.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
            //过滤数据，并对数据按时间进行排序
            Stream<BootStrapExpressData> statu0Express = list1.stream().filter(express -> {
                if (express.getStatus().equals("待取件")){
                    return true;
                }else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);

            });
            //过滤数据，并对数据按时间进行排序
            Stream<BootStrapExpressData> statu1Express = list1.stream().filter(express -> {
                if (express.getStatus().equals("已取件")){
                    return true;
                }else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });

            Object[] s0 = statu0Express.toArray();
            Object[] s1 = statu1Express.toArray();
            Map data = new HashMap<>();
            data.put("status0",s0);
            data.put("status1",s1);
            msg.setData(data);
        }

        String json = JsonUtil.toJSON(msg.getData());

        return json;
    }



    @ResponseBody("/WxExpress/addExpress.do")
    public String insert(HttpServletRequest req,HttpServletResponse resp){
        //获取前端返回的录入的信息
        String number = req.getParameter("number");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String company = req.getParameter("company");
        //从session中获取登入用户的手机号，即录入快递人员的手机号
        String[] userInfo = (String[]) req.getSession().getAttribute("userInfo");
        String sysPhone = userInfo[0];

        Express express = new Express();
        express.setNumber(number);
        express.setUserName(userName);
        express.setUserPhone(userPhone);
        express.setCompany(company);
        express.setSysPhone(sysPhone);

        Message msg = new Message();
        try {
            boolean flag = service.insert(express);

            if (flag){
                msg.setStatus(0);
                msg.setResult("快递录入成功");
            }else {
                msg.setStatus(-1);
                msg.setResult("快递录入失败");
            }
        } catch (ReCodeException e) {
            e.printStackTrace();
        }

        return JsonUtil.toJSON(msg);

    }

    @ResponseBody("/WxExpress/findByCode.do")
    public String findByCode(HttpServletRequest req,HttpServletResponse resp){
        //获取前端给的取货码
        String code = req.getParameter("code");
        Express e = null;
        try {
            e = service.findByCode(code);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        Message msg = new Message();
        if (e != null && e.getId()!=0){
            msg.setStatus(0);
            msg.setResult("快件查询成功");
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code1 = e.getCode()==null?"快递柜空闲":e.getCode();
            BootStrapExpressData e1 = new BootStrapExpressData(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code1,inTime,outTime,status,e.getSysPhone());

            msg.setData(e1);
        }else {
            msg.setStatus(-1);
            msg.setResult("未查询到快递信息");

        }

        return JsonUtil.toJSON(msg);

    }

    @ResponseBody("/WxExpress/updateCodeStatus.do")
    public String updateCodeStatus(HttpServletRequest req,HttpServletResponse resp){
        //从前端获取取货码
        String code = req.getParameter("code");
        String number = req.getParameter("number");
        //取件后修改快递状态
        boolean flag = service.updateStatus(code);
        Message msg = new Message();
        if (flag){
            //取件成功
            msg.setStatus(0);
            msg.setResult("取件成功");
            //通过快递单号获取当前快递信息
            Express e = service.findByNumber(number);
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code1 = e.getCode()==null?"快递柜空闲":e.getCode();
            BootStrapExpressData e1 = new BootStrapExpressData(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code1,inTime,outTime,status,e.getSysPhone());

            msg.setData(e1);

        }else {
            //取件失败
            msg.setStatus(-1);
            msg.setResult("取件失败");
        }

        return JsonUtil.toJSON(msg);

    }


}
